#pragma once
#include <QGraphicsRectItem>
#include <QGraphicsSceneMouseEvent>
#include <QCursor>
#include "qcustomviews_global.h"

class QCUSTOMVIEWS_EXPORT CustomRectItem :
    public QGraphicsRectItem
{
public:
    CustomRectItem(QGraphicsItem *parent = nullptr);

    CustomRectItem(const QRectF& rect, QGraphicsItem* parent = nullptr);
    CustomRectItem(qreal x, qreal y, qreal w, qreal h, QGraphicsItem* parent = nullptr);
protected:
    //QVariant itemChange(GraphicsItemChange change, const QVariant& value) override;

    // 鼠标按下事件
    void mousePressEvent(QGraphicsSceneMouseEvent* event) override;   
    

    // 鼠标移动事件
    void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override;
    

    // 鼠标释放事件
    void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override;
    

    //// 鼠标悬停事件
    //void hoverMoveEvent(QGraphicsSceneHoverEvent* event) override;

    void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override;
    

private:

    bool isResizeHandle(const QPointF& pos)
    {
        // 判断鼠标位置是否在缩放手柄区域内
        QRectF handleRect(rect().right() - resizeHandleSize, rect().bottom() - resizeHandleSize, resizeHandleSize, resizeHandleSize);
        return handleRect.contains(pos);
    }

    bool isDragging;
    bool isResizing;
    QPointF dragStartPos;
    QPointF resizeStartPos;
    QRectF resizeStartRect;
    qreal resizeHandleSize;

    QPointF startPoint_;
    QRectF startRect_;
};

